package com.ln.lnzw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.ln.lnzw.R;
import com.ln.lnzw.activity.LegalPersonRegisteredActivity;
import com.ln.lnzw.activity.RegisteredActivity;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseFragment;
import com.ln.lnzw.bean.LoginBean;
import com.ln.lnzw.net.HttpMethods;
import com.ln.lnzw.utils.ToastFactory;
import com.ln.lnzw.utils.TokenUtils;
import com.ln.lnzw.view.ClearableEditText;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/4/10.
 * 法人登录
 */

public class LegalPersonLoginFragment extends BaseFragment {

    @BindView(R.id.cet_phone)
    ClearableEditText cetPhone;
    @BindView(R.id.cet_mima)
    ClearableEditText cetMima;
    @BindView(R.id.tv_wangjimima)
    TextView tvWangjimima;
    @BindView(R.id.rl_tijaio)
    RelativeLayout rlTijaio;
    @BindView(R.id.tv_lijizhuce)
    TextView tvLijizhuce;
    Unbinder unbinder;
    private String loginWay="1";
    private String loginMold="";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.login_activity, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setView();

    }
    private void setView() {
        cetPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });
        cetMima.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });

    }
    private void editTextIsNo() {
        if (TextUtils.isEmpty(cetPhone.getText())||TextUtils.isEmpty(cetMima.getText())){
            rlTijaio.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
        }
    }

    private void editTextIsYes() {
        if (!TextUtils.isEmpty(cetPhone.getText())&&!TextUtils.isEmpty(cetMima.getText())){
            rlTijaio.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_wangjimima, R.id.rl_tijaio, R.id.tv_lijizhuce})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_wangjimima:
                break;
            case R.id.rl_tijaio:
                if (TextUtils.isEmpty(cetPhone.getText())||TextUtils.isEmpty(cetMima.getText())){
                    return;
                }
                if (!TextUtils.isEmpty(cetPhone.getText())&&!TextUtils.isEmpty(cetMima.getText())){
//                    if (!RegexUtils.isMobileExact(cetPhone.getText())){
//                        ToastFactory.getToast(activity, "请输入正确的手机号").show();
//                        return;
//                    }
                }

                //请求登录接口 保存返回的用户信息
                initData();
                break;
            case R.id.tv_lijizhuce:
                ActivityUtils.startActivity(activity, LegalPersonRegisteredActivity.class);
                break;
        }
    }
    private void initData() {
        Log.i("555", "initData: "+cetPhone.getText().toString());
        Log.i("555", "initData: "+cetMima.getText().toString());
        HttpMethods.getInstanceCenter().info.getLegalPersonLogin(TokenUtils.getInstance().GetGUID(),cetPhone.getText().toString(),cetMima.getText().toString(),"")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
//                        Log.i("555", "onNext: "+loginBean.toString());
                        if ("200".equals(loginBean.getCode())){
                            ToastFactory.getToast(activity,loginBean.getMsg()).show();
//                            Log.i("555", "onNext: "+loginBean.toString());
                            activity.spUtils.put(AppConstant.USER_NAME,loginBean.getResult().getTokenInfo().getLoginName());
                            activity.spUtils.put(AppConstant.USER_PIC,"");
                            activity.spUtils.put(AppConstant.TOKEN,loginBean.getResult().getTokenInfo().getToken());
                            Log.i("555", "TOKEN: "+loginBean.getResult().getTokenInfo().getToken());
                            activity.spUtils.put(AppConstant.USER_UID,loginBean.getResult().getTokenInfo().getLoginUuid());
                            Log.i("555", "USER_UID: "+loginBean.getResult().getTokenInfo().getLoginUuid());
                            activity.spUtils.put(AppConstant.USER_CREDITID,loginBean.getResult().getUserInfo().getCreditID());
                            activity.spUtils.put(AppConstant.TRUE_NAME,loginBean.getResult().getUserInfo().getTrueName());
                            activity.spUtils.put(AppConstant.MOBILE,loginBean.getResult().getUserInfo().getMobile());
                            activity.spUtils.put(AppConstant.EMAIL,loginBean.getResult().getUserInfo().getEmail());
                            Log.i("555", "TOKEN: "+loginBean.getResult().getUserInfo().getCreditID());
                            Log.i("555", "TOKEN: "+loginBean.getResult().getUserInfo().getTrueName());
                            Log.i("555", "TOKEN: "+loginBean.getResult().getUserInfo().getMobile());
                            Log.i("555", "TOKEN: "+loginBean.getResult().getUserInfo().getEmail());

                        }
                        if ("500".equals(loginBean.getCode())){
                            ToastFactory.getToast(activity,loginBean.getMsg()).show();
                            return;
                        }


                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("555", "onError: "+e.toString());
                    }

                    @Override
                    public void onComplete() {
                        EventBus.getDefault().post("login");
//                        CacheActivity.finishActivity();
                        getActivity().finish();
                    }
                });
    }

}
